<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.remark" @input="(val)=>{ return val.trim()}" placeholder="清分流水号" class="filter-item" clearable/>
      <div class="filter-item">
        <div class="searchItem">
          <span class="searchLabel">支付日期：</span>
          <DatePicker :mode="4" :startDate.sync="listQuery.payCompleteDate$gte" :endDate.sync="listQuery.payCompleteDate$lte" clearable />
        </div>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="success" icon="el-icon-document" @click="handleDownload">
        导出
      </el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;" :max-height="tableHeight">
      <el-table-column label="支付日期" prop="payCompleteDate" align="center" />
      <el-table-column label="订单号" prop="zcgjOriginOrderIdentifier" align="center">
        <!-- <template slot-scope="{row}">
          <div v-if="row.zcgjOriginOrderIdentifier.substring(0, 1) == 'Z'" @click="clickDetail(row)" style="color: rgba(14, 131, 192, 1); cursor: pointer;">{{ row.zcgjOriginOrderIdentifier }}</div>
          <div v-else>{{ row.zcgjOriginOrderIdentifier }}</div>
        </template> -->
      </el-table-column>
      <el-table-column label="单号" prop="zcgjOrderIdentifier" align="center" />
      <el-table-column label="商品名称" prop="detail" align="center" />
      <el-table-column label="订单总数量" prop="totalNum" align="center" />
      <el-table-column label="业务种类" align="center">
        <template slot-scope="{row}">
          {{ row.trxType | payFilter }}
        </template>
      </el-table-column>
      <el-table-column label="交易金额" prop="withdrawAmtBigDecimal" align="center"/>
      <el-table-column label="交易流水号" prop="serialNo" align="center" />
      <el-table-column label="清分流水号" prop="remark" align="center" />
    </el-table>
    <Pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import DatePicker from '@/components/DatePicker'
import { b2cSettlementPageByQo } from '@/api/bank'
import updateTableMixin from '@/mixin/updateTable'

export default {
  name: 'xcxSettlement',
  directives: { waves },
  props: {},
  components: {
    Pagination,
    DatePicker
  },
  filters: {
    payFilter(status) {
      const statusMap = {
        '01': '主动结算',
        '02': '自动结算',
        '03': '强制结算',
        '04': '佣金结算',
        '05': '平台补贴结算'
      }
      return statusMap[status]
    }
  },
  mixins: [updateTableMixin],
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      downloadLoading: false,
      listQuery: {
        remark: '',
        payCompleteDate$gte: '',
        payCompleteDate$lte: '',
        pageNo: 1,
        pageSize: 20
      },
      allListQuery: {
        payCompleteDate$gte: '',
        payCompleteDate$lte: '',
        pageNo: 1,
        pageSize: 20
      },
    }
  },
  computed: {},
  watch: {},
  methods: {
    clickDetail(row) {
      this.$router.push({ 
        path: '/purchaseOrder/zcyxOrder', 
        query: {
          identifier:row.zcgjOriginOrderIdentifier, 
          status: 0, 
          t: Date.now()
        } 
      })
    },
    getList() {
      this.listLoading = true
      b2cSettlementPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
        this.updateTableHeight()
      }).catch(error => {
        console.log(error);
        this.listLoading = false
      })
    },
    getAllList() {
      this.listLoading = true
      this.allListQuery = Object.assign({}, this.listQuery);
      this.allListQuery.pageNo = 1;
      this.allListQuery.pageSize = this.total

      return new Promise((resolve, reject) => {
        b2cSettlementPageByQo(this.allListQuery).then(response => {
          this.allList = response.list || []
          this.listLoading = false
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleDownload() {
      this.getAllList().then(res => {
        if (this.allList.length <= 0) {
          return;
        }

        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['支付日期', '订单号', '单号', '商品名称', '订单总数量', '业务种类', '交易金额', '交易流水号', '清分流水号']
          const filterVal = ['payCompleteDate', 'zcgjOriginOrderIdentifier', 'zcgjOrderIdentifier', 'detail', 'totalNum', 'trxType', 'withdrawAmtBigDecimal', 'serialNo', 'remark']
          const list = this.allList
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: "小程序结算账单",
            autoWidth: true,
            bookType: "xlsx"
          })
          this.downloadLoading = false
        })
      });
    },
    formatJson(filterVal, jsonData) {
        return jsonData.map(v => {
          return filterVal.map(j => {
            if (j === 'trxType') {
              if (v[j] == '01') {
                return '主动结算'
              } else if (v[j] == '02') {
                return '自动结算'
              } else if (v[j] == '03') {
                return '强制结算'
              } else if (v[j] == '04') {
                return '佣金结算'
              } else {
                return '平台补贴结算'
              }
            } else {
              return v[j]
            }
          })
        })
      },
  },
  created() {
    this.getList()
  },
  mounted() {
    
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
}
</script>

<style lang='scss' scoped>
  .searchItem {
    display: flex;
    align-items: center;
    .searchLabel {
      color: #666;
    }
  }
</style>